<?php
/**
 * Created by PhpStorm.
 * User: jiangshuxiong
 * Date: 2018/1/26
 * Time: 13:52
 */

$con1 = mysqli_connect("localhost","root","123");
$create_tb = "CREATE TABLE my_tb1(" .
              "id INT NOT NULL AUTO_INCREMENT," .
              "title VARCHAR(100) NOT NULL," .
              "author VARCHAR(100) NOT NULL," .
              "submission_date DATE," .
              "PRIMARY KEY(id)) default charset=utf8;";
if(!$con1){
    echo "connect failure <br/>";
}else{
    echo "connect success <br />";
    //create db
    if(mysqli_query($con1,"create database my_db2")){
        echo "create success <br />";
        if(mysqli_select_db($con1,"my_db2")){
            echo "select success <br />";
            //create tb
            $cr_tb = mysqli_query($con1,$create_tb);
            if(!$cr_tb){
                echo "create table failure. <br />";
            }else{
                echo "create table success <br />";
                //insert data
                $now_date = date("Y/m/d");
                $title = 'clyde';
                $author = "江";
                $insert_data = "INSERT INTO my_tb1(title,author,submission_date)".
                               "VALUES ('$title','$author','$now_date');";
                if(mysqli_query($con1,$insert_data)){
                    echo "insert data success <br />";
                }else{
                    echo "insert data failure <br />";
                }

                echo "insert next<br />";
                $now_date1 = date("Y/m/d");
                $title1 = "jerry";
                $author1 = "苏";
                $insert_data1 = "INSERT INTO my_tb1(title,author,submission_date)".
                    "VALUES ('$title1','$author1','$now_date1');";
                if(mysqli_query($con1,$insert_data1)){
                    echo "insert data success <br />";
                }else{
                    echo "insert data failure <br />";
                }

                //search: mysqli_fetch_array
                $slct = "SELECT * FROM my_db2.my_tb1;";
                $result = mysqli_query($con1,$slct);
               // if($$result){
                    echo "----------------<br />";
                    while($row = mysqli_fetch_array($result, MYSQLI_BOTH))
                    {
                        echo " ID :{$row[0]}  <br> ".
                            "Title: {$row[1]} <br> ".
                            "Author: {$row[2]} <br> ".
                            "Submission Date : {$row[3]} <br> ".
                            "--------------------------------<br>";
                    }
                    echo "----------------<br />";
                //}



            }

            if(mysqli_query($con1,"DROP TABLE my_tb1")){
                echo "drop success <br />";
            }else{
                echo "drop failure <br />";
            }
            if(mysqli_query($con1,"DROP DATABASE my_db2")){
                echo "drop database success <br />";
            }else{
                echo "drop failure <br />";
            }

        }else{
            echo "select failure <br />";
        }

    }else{
        echo "create database failure <br />";
    }
}
echo "end.";
mysqli_close($con1);